package com.dataseek.iot.mysql.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dataseek.iot.core.constant.SystemConst;
import com.dataseek.iot.mysql.entity.LoginUser;
import com.dataseek.iot.mysql.entity.Role;
import com.dataseek.iot.mysql.entity.User;
import com.dataseek.iot.mysql.mapper.UserMapper;
import com.dataseek.iot.mysql.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author cc
 * @since 2021-09-05
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {


    @Autowired
    RoleServiceImpl roleService;



    @Override
    public User findByUsername(String username) {
        QueryWrapper wrapper  = new QueryWrapper();
        wrapper.eq("username", username);
        return super.getOne(wrapper);
    }


    public boolean save(User entity) {
        return super.save(entity);
    }

    /**
     * 根据用户名称获取用户详情
     * @param username
     * @return
     */
    public LoginUser selectAllUserDetails( String username){
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("username", username);
        //用户信息
        User user = this.getOne(wrapper);
        LoginUser loginUser = new LoginUser();
        loginUser.setAvatar(user.getAvatar());
        loginUser.setUserId(user.getUserInnerId());
        loginUser.setComment(user.getComment());
        loginUser.setCreateTime(user.getCreateTime());
        loginUser.setUsername(user.getUsername());
        loginUser.setPassword(user.getPassword());
        loginUser.setNickname(user.getNickname());
        loginUser.setLineCode(user.getLineCode());
        loginUser.setMail(user.getMail());
        loginUser.setTel(user.getTel());
        loginUser.setUsed(user.getUsed());
        loginUser.setSex(user.getSex());
        //角色信息
        String roleId = user.getRoleId();
        Role role = roleService.getById(roleId);
        loginUser.setRoleId(role.getRoleInnerId().toString());
        loginUser.setRoleCode(role.getRoleCode());
        loginUser.setRoleName(role.getRoleName());
        loginUser.setRoutes(role.getRoutes());
        loginUser.setMenuAuth(role.getMenuAuth());
        //设备信息
        loginUser.setLineCode(user.getLineCode());
        loginUser.setLineName(user.getLineName());
        if(!role.getRoleCode().equals(SystemConst.SUPER_USER)){
            //区域信息
            JSONArray info = JSON.parseArray(user.getLineName());
            List<Object> areas = new ArrayList<>();
            for( Object s : info){
                JSONObject json = (JSONObject)s;
                String areaName = json.getString("areaName");
                String[] temp = areaName.split("-");
                String[] finalArea;
                if(temp.length>3){
                    finalArea = Arrays.copyOf(temp, temp.length-1);
                }else{
                    finalArea = Arrays.copyOf(temp, temp.length);
                }
                areas.add(finalArea);
                loginUser.setArea(areas);
            }
        }

        return loginUser;
    }

    /**
     * 根据角色ID查询用户
     * @param id
     * @return
     */
    public List<User> getUsersByRoleId(String id){
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("role_id", id);
        return this.list(wrapper);
    }

    /**
     * 根据线路编码查询用
     * @param code
     * @return
     */
    public List<User> getUsersByLineCode(String code){
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.like("line_code", code);
        return this.list(wrapper);
    }



}
